package se.miun.datatek;


import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
*
* @author Patrik Jansson
* 
*/
public class AddChildDialog extends Dialog implements OnClickListener
{
	
	//Child
	private EditText nameInput;
	private EditText sinInput;
	private EditText phoneOneInput;
	private EditText phoneTwoInput;
	private EditText allergyInput;
	
	//Caretaker one
	private EditText caretakerOneInput;
	private EditText caretakerOneWorkInput;
	private EditText caretakerOneWorkPlacePhoneInput;
	private EditText caretakerOneMobilePhoneInput;
	private EditText caretakerOneEmail;
	
	//Caretaker two
	private EditText caretakerTwoInput;
	private EditText caretakerTwoWorkInput;
	private EditText caretakerTwoWorkPlacePhoneInput;
	private EditText caretakerTwoMobilePhoneInput;
	private EditText caretakerTwoEmail;
	
	//Dialog Buttons
	private Button doneButton;
	private Button abortButton;
	
	private Child child;
	

	
	public AddChildDialog(Context context) 
	{
		
		super(context);
		setContentView(R.layout.edit_and_create_child_layout);
		setTitle("Redigera");
		
		
		//Child
		nameInput = (EditText) findViewById(R.id.nameInput);
		sinInput = (EditText) findViewById(R.id.sinInput);
		phoneOneInput = (EditText) findViewById(R.id.phoneOneInput);
		phoneTwoInput = (EditText) findViewById(R.id.phoneTwoInput);
		allergyInput = (EditText) findViewById(R.id.allergyInput);
		
		
		//Caretaker one
		caretakerOneInput = (EditText) findViewById(R.id.caretakerOneInput);
		caretakerOneWorkInput = (EditText) findViewById(R.id.caretakerOneWorkInput);
		caretakerOneWorkPlacePhoneInput = (EditText) findViewById(R.id.caretakerOneWorkPlacePhoneInput);
		caretakerOneMobilePhoneInput = (EditText) findViewById(R.id.caretakerOneMobilePhoneInput);
		caretakerOneEmail = (EditText) findViewById(R.id.caretakerOneEmail);
		
		
		//Caretaker two
		caretakerTwoInput = (EditText) findViewById(R.id.caretakerTwoInput);
		caretakerTwoWorkInput = (EditText) findViewById(R.id.caretakerTwoWorkInput);
		caretakerTwoWorkPlacePhoneInput = (EditText) findViewById(R.id.caretakerTwoWorkPlacePhoneInput);
		caretakerTwoMobilePhoneInput = (EditText) findViewById(R.id.caretakerTwoMobilePhoneInput);
		caretakerTwoEmail = (EditText) findViewById(R.id.caretakerTwoEmail);
		
		
		//Dialog Buttons
		doneButton = (Button) findViewById(R.id.doneButton);
		abortButton = (Button) findViewById(R.id.abortButton);
		doneButton.setOnClickListener(this);
		abortButton.setOnClickListener(this);
		
		
		//Add data from database in the appropriate EditText for display in the form
		//child = SQLiteTestActivity.dataSource.getChild(sin_num);
		
		/*nameInput.setText(child.getName());
		sinInput.setText(child.getSin_num());
		phoneOneInput.setText(child.getHome_phone_number());
		phoneTwoInput.setText(child.getHome_phone_number2());
		allergyInput.setText(child.getAllergy());
		
		caretakerOneInput.setText(child.getCaretaker_1());
		caretakerOneWorkInput.setText(child.getWorkplace_1());
		caretakerOneWorkPlacePhoneInput.setText(child.getPhone_workplace_1());
		caretakerOneMobilePhoneInput.setText(child.getMobile_phone_1());
		caretakerOneEmail.setText(child.getEmail_1());
		
		caretakerTwoInput.setText(child.getCaretaker_2());
		caretakerTwoWorkInput.setText(child.getWorkplace_2());
		caretakerTwoWorkPlacePhoneInput.setText(child.getPhone_workplace_2());
		caretakerTwoMobilePhoneInput.setText(child.getMobile_phone_2());
		caretakerTwoEmail.setText(child.getEmail_2());*/
		
		
	}

	
	public void onClick(View v) 
	{
		
		//Take inputs and send to database
		if(v.getId() == doneButton.getId())
		{	
			child = new Child();
			child.setID(SQLiteTestActivity.dataSource.highestIDInDatabase()+1);
			child.setName(nameInput.getText().toString());
			child.setSin_num(sinInput.getText().toString());
			child.setHome_phone_number(phoneOneInput.getText().toString());
			child.setHome_phone_number2(phoneTwoInput.getText().toString());
			child.setAllergy(allergyInput.getText().toString());
			
			
			child.setCaretaker_1(caretakerOneInput.getText().toString());
			child.setWorkplace_1(caretakerOneWorkInput.getText().toString());
			child.setPhone_workplace_1(caretakerOneWorkPlacePhoneInput.getText().toString());
			child.setMobile_phone_1(caretakerOneMobilePhoneInput.getText().toString());
			child.setEmail_1(caretakerOneEmail.getText().toString());
			
			
			child.setCaretaker_2(caretakerTwoInput.getText().toString());
			child.setWorkplace_2(caretakerTwoWorkInput.getText().toString());
			child.setPhone_workplace_2(caretakerTwoWorkPlacePhoneInput.getText().toString());
			child.setMobile_phone_2(caretakerTwoMobilePhoneInput.getText().toString());
			child.setEmail_2(caretakerTwoEmail.getText().toString());
			
			
			SQLiteTestActivity.dataSource.addChildToDatabase(child);
			this.dismiss();
		}
		
		
		if(v.getId() == abortButton.getId())
		{
			this.dismiss();		
		}
		
		
		
	}
	
	

}